// pages/giftCard/cardBuyResult/cardBuyResult.js
const giftCardService = require('../../../../api/giftCardService.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrl: '',
    name: '',
    state: '',
    memberCode: '',
    cardId: '',
    cardNo: '',
    allowPresent: '',
    recharge: '',
    created: '',
    state: '',
    fromSend: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.cardNo) {
      giftCardService.queryMyGiftCardDetails(options.cardNo).then(res => {
        console.log(res)
        if (res.myGiftCardDTO) {
          this.setData({
            imageUrl: res.myGiftCardDTO.imageUrl,
            name: res.myGiftCardDTO.name,
            state: res.myGiftCardDTO.state,
            memberCode: res.myGiftCardDTO.memberCode,
            cardId: res.myGiftCardDTO.cardId,
            cardNo: res.myGiftCardDTO.cardNo,
            allowPresent: res.myGiftCardDTO.allowPresent,
            recharge: res.myGiftCardDTO.recharge,
            created: res.myGiftCardDTO.created,
            state: res.myGiftCardDTO.state
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000
        })
      })
    }
    if (options.fromSend) {
      this.setData({
        fromSend: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var self = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      // console.log(res.target)
      wx.showShareMenu({
        withShareTicket: true
      })
    }
    return {
      title: '送你一张礼品卡',
      path: "/pages/memberModule/giftCard/giveState/giveState?cardNo=" + self.data.cardNo,
      success: function (res) {
        if (res.errMsg == 'shareAppMessage:ok') {
          giftCardService.donate(self.data.cardNo).then(res => {
            console.log('已经赠送————————————————')
            console.log(res)
          }).catch(err => {
            wx.showToast({
              title: err.message,
              icon: 'none',
              duration: 2000
            })
          })
        }
      }
    }
  },

  /**
   * 激活自用
   */
  handelActivate: function (e) {
    console.log(e)
    if (this.data.cardNo) {
      giftCardService.active(this.data.cardNo).then(res => {
        console.log(res)
        wx.navigateTo({
          url: '../giftCardDetails/giftCardDetails?cardNo=' + this.data.cardNo,
        })
      }).catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000
        })
      })
    }
  }
})